package com.cszjzx.member.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

public class QRCode {
	static int width = 200; 
	static int height = 200; 
	 public QRCode() 
	 { 
	 } 
	 
	 public void create_image(String sms_info,String path,String idcard)throws Exception{ 
		  try{ 
		        Qrcode testQrcode =new Qrcode(); 
		            testQrcode.setQrcodeErrorCorrect('M'); 
		            testQrcode.setQrcodeEncodeMode('B'); 
		            testQrcode.setQrcodeVersion(20); 
		            String testString = sms_info; 
		            byte[] d = testString.getBytes("gbk"); 
		            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
		            Graphics2D g = bi.createGraphics(); 
		            g.setBackground(Color.WHITE); 
		            g.clearRect(0, 0, width, height); 
		            g.setColor(Color.BLACK); 
		            // 限制最大字节数为119 
		            if (d.length>0){ 
		                boolean[][] s = testQrcode.calQrcode(d); 
		                for (int i=0;i<s.length;i++){ 
		                    for (int j=0;j<s.length;j++){ 
		                        if (s[j][i]) { 
		                            g.fillRect(j*2,i*2,2,2); 
		                        } 
		                    } 
		                } 
		            } 
		            g.dispose(); 
		            bi.flush(); 
		            File f = new File(path+idcard+".jpg"); 
		            if(!f.exists()) f.createNewFile(); 
		            ImageIO.write(bi, "jpg", f); 
		        } 
		        catch (Exception e) { 
		            e.printStackTrace(); 
		        }  
		 } 
}
